from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime, Boolean, Text, ForeignKey, Enum, Float, JSON
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
import enum
from app.db.database import Base

class ItemType(str, enum.Enum):
    LOST = "lost"  # 丢失物品
    FOUND = "found"  # 找到物品

class ItemStatus(str, enum.Enum):
    PENDING = "pending"  # 待处理
    CLAIMED = "claimed"  # 已认领
    COMPLETED = "completed"  # 已完成
    CANCELLED = "cancelled"  # 已取消

class Item(Base):
    __tablename__ = "items"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(100), nullable=False)
    description = Column(Text)
    item_type = Column(Enum(ItemType), nullable=False)
    status = Column(Enum(ItemStatus), default=ItemStatus.PENDING)
    location = Column(String(255))
    image_url = Column(String(500))
    reward = Column(String(100))
    contact_info = Column(String(255))
    
    # 外键关系
    owner_id = Column(Integer, ForeignKey("users.id"))
    finder_id = Column(Integer, ForeignKey("users.id"))
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # 关联关系
    owner = relationship("User", back_populates="lost_items", foreign_keys=[owner_id])
    finder = relationship("User", back_populates="found_items", foreign_keys=[finder_id])
    claims = relationship("Claim", back_populates="item")

class ClaimStatus(str, enum.Enum):
    PENDING = "pending"  # 待处理
    APPROVED = "approved"  # 已通过
    REJECTED = "rejected"  # 已拒绝
    CANCELLED = "cancelled"  # 已取消

class Claim(Base):
    __tablename__ = "claims"

    id = Column(Integer, primary_key=True, index=True)
    item_id = Column(Integer, ForeignKey("items.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    status = Column(Enum(ClaimStatus), default=ClaimStatus.PENDING)
    description = Column(Text)
    proof_image_url = Column(String(500))
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # 关联关系
    item = relationship("Item", back_populates="claims")
    user = relationship("User", back_populates="claims")
